package com.example.demomybatis.service.impl;

import com.example.demomybatis.entity.Course;
import com.example.demomybatis.mapper.CourseMapper;
import com.example.demomybatis.service.CourseService;
import com.example.demomybatis.vo.RequestCoursePr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    RedisTemplate redisTemplate;

    private static final String KEY = "all_course";

    @Override
    @CachePut(value = "course",key = "#course.courseId")
    public Course addCourse(Course course) {
        courseMapper.addCourse(course);
        return course;
    }

    @Override
    public Integer updateCourse(Course course) {
        Integer result = courseMapper.updateCourse(course);
        return result;
    }

    @Override
    public List<Course> selectCourse(RequestCoursePr requestCoursePr) {
        List<Course> courseList = courseMapper.selectCourse(requestCoursePr);
        return courseList;
    }

    @Override
    @CacheEvict(value = "course", key = "#courseId")
    public Integer deleteCourse(Integer courseId) {
        //根据ID删除记录
        Integer result = courseMapper.deleteCourse(courseId);
        return result;
    }

    @Override
    @Cacheable(value = "course",key = "#courseId")
    public Course selectOneCourse(Integer courseId) {
        Course course = courseMapper.selectOneCourse(courseId);
        return course;
    }

    @Override
    public Integer selectCountCourse(RequestCoursePr requestCoursePr) {
        return courseMapper.selectCountCourse(requestCoursePr);
    }
}
